using Microsoft.EntityFrameworkCore;
using Universal.Domain.Entities.App;
using Universal.Infrastructure.DB.Config;

namespace Universal.Infrastructure.DB;

/// <summary>
/// 数据库上下文
/// </summary>
public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
    public DbSet<AppUser> Users { get; set; }
    public DbSet<AppRole> Roles { get; set; }
    public DbSet<AppMenu> Menus { get; set; }
    public DbSet<AppPermission> Permissions { get; set; }
    public DbSet<AppFile> Files { get; set; }
    public DbSet<AppArticle> Articles { get; set; }
    public DbSet<AppArticleCategory> ArticleCategories { get; set; }
    public DbSet<SystemConfigs> SystemConfigs { get; set; }

    public DbSet<AppLog> Logs { get; set; }

    // 配置实体
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // 全局字段、表名自动 snake_case
        modelBuilder.ApplyCustomNamingConventions();

        // 注册所有实体的配置类
        modelBuilder.ApplyConfiguration(new AppUserConfiguration());
        modelBuilder.ApplyConfiguration(new AppRoleConfiguration());
        modelBuilder.ApplyConfiguration(new AppMenuConfiguration());
        modelBuilder.ApplyConfiguration(new AppArticleConfiguration());
        modelBuilder.ApplyConfiguration(new AppArticleCategoryConfiguration());
        // 其他如 AppFile、AppPermission、SystemConfigs 若无特殊关系可不写配置类
    }

}